package com.zy.fee.roadpark.dao;

import android.content.Context;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Response;
import com.yanzhenjie.nohttp.rest.SimpleResponseListener;
import com.yanzhenjie.nohttp.rest.StringRequest;
import com.zy.fee.roadpark.config.Urls;
import com.zy.fee.roadpark.model.apimodel.APIM_CarNumberList;
import com.zy.fee.roadpark.model.apimodel.APIM_EscapeItemList;
import com.zy.fee.roadpark.model.apimodel.APIM_EscapeList;
import com.zy.fee.roadpark.model.apimodel.APIM_NodeRecordInfo;
import com.zy.fee.roadpark.model.apimodel.APIM_NpNodeRecordList;
import com.zygk.library.model.CommonResult;
import com.zygk.library.util.CallServer;
import com.zygk.library.util.HttpRequest;
import com.zygk.library.util.JsonUtil;
import com.zygk.library.util.PreferencesHelper;
import com.zygk.library.util.ToastUtil;

/**
 * Created by Administrator on 2018/9/13 0013.
 */

public class NpNodeRecordLogic {
  /**
   * 停车场获取未支付的停车单列表
   */
  public static void npnode_getEscapeList(final Context mContext, String lotID, String carNumber,
      int page, int rows, final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NPNODE_GET_ESCAPE_LIST, RequestMethod.POST);
    request.add("lotID", lotID);
    request.add("carNumber", carNumber);
    request.add("page", page);
    request.add("rows", rows);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        APIM_EscapeList result = JsonUtil.jsonToObject(response.get(), APIM_EscapeList.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 根据车牌号获取未支付的停车单列表
   */
  public static void npnode_getCarEscapeList(final Context mContext, String carNumber,
      int page, int rows, final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NPNODE_GET_CAR_ESCAPE_LIST, RequestMethod.POST);
    request.add("carNumber", carNumber);
    request.add("page", page);
    request.add("rows", rows);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        APIM_EscapeItemList result = JsonUtil.jsonToObject(response.get(), APIM_EscapeItemList.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 获取地磁停车单详情
   */
  public static void npnode_record_info(final Context mContext, String nodeRecordID,
      final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NPNODE_RECORD_INFO, RequestMethod.POST);
    request.add("nodeRecordID", nodeRecordID);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        APIM_NodeRecordInfo result =
            JsonUtil.jsonToObject(response.get(), APIM_NodeRecordInfo.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 管理员确认线下收款
   */
  public static void npnode_record_pay_admin(final Context mContext, String adminID,
      String nodeRecordID, double payMoney, final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NPNODE_RECORD_PAY_ADMIN, RequestMethod.POST);
    request.add("adminID", adminID);
    request.add("nodeRecordID", nodeRecordID);
    request.add("payMoney", payMoney);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        CommonResult result = JsonUtil.jsonToObject(response.get(), CommonResult.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 逃逸单收款
   */
  public static void escape_pay_admin(final Context mContext, String adminID, String nodeRecordID,
      double payMoney, final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NPNODE_RECORD_ESCAPEPAY_ADMIN, RequestMethod.POST);
    request.add("adminID", adminID);
    request.add("nodeRecordID", nodeRecordID);
    request.add("payMoney", payMoney);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        CommonResult result = JsonUtil.jsonToObject(response.get(), CommonResult.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 收款(根据车位号和车牌号获取未支付的最新的停车单)
   */
  public static void nbnode_searchrecord_unpay(final Context mContext, String adminID, String code,
      String carNumber, final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NBNODE_SEARCHRECORD_UNPAY, RequestMethod.POST);
    request.add("adminID", adminID);
    request.add("code", code);
    request.add("carNumber", carNumber);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        APIM_NodeRecordInfo result =
            JsonUtil.jsonToObject(response.get(), APIM_NodeRecordInfo.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 获取管理员管理的地磁设备中正在停车的且已取证的所有车辆的所有历史欠费单据。
   */
  public static void nbnode_record_histroy_admin_list(final Context mContext,
      final HttpRequest.HttpCallback callback) {
    StringRequest request =
        new StringRequest(Urls.NBNODE_RECORD_HISTROY_ADMIN_LIST, RequestMethod.POST);
    request.add("adminID", PreferencesHelper.userManager().getUserID());
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        APIM_CarNumberList result = JsonUtil.jsonToObject(response.get(), APIM_CarNumberList.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 追缴单详情
   */
  public static void escapeRecordInfo(final Context mContext, String nodeRecordID,
      final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NPNODE_ESCAPE_RECORD_INFO, RequestMethod.POST);
    request.add("nodeRecordID", nodeRecordID);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        APIM_NodeRecordInfo result =
            JsonUtil.jsonToObject(response.get(), APIM_NodeRecordInfo.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 根据车牌判断是否有逃逸订单
   */
  public static void npnode_whether_escape(final Context mContext, String carNumber,
      final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NPNODE_WHETHERESCAPE, RequestMethod.POST);
    request.add("carNumber", carNumber);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        CommonResult result = JsonUtil.jsonToObject(response.get(), CommonResult.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 设置逃逸订单
   */
  public static void npnode_set_escape(final Context mContext, String nodeRecordID,
      final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NPNODE_SET_ESCAPE, RequestMethod.POST);
    request.add("nodeRecordID", nodeRecordID);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        CommonResult result = JsonUtil.jsonToObject(response.get(), CommonResult.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 根据停车场和车牌查找停车单ID
   */
  public static void npnode_get_node_record_id(final Context mContext, String lotID,
      String carNumber, final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NPNODE_GET_NODE_RECORD_INFO, RequestMethod.POST);
    request.add("lotID", lotID);
    request.add("carNumber", carNumber);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        CommonResult result = JsonUtil.jsonToObject(response.get(), CommonResult.class);
        if (result == null) return;
        callback.onSucceed(result);
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 获取无地磁收费扫码二维码地址
   */
  public static void getQrUrl(final Context mContext, String nodeRecordID, final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NPNODE_GET_QRCODEURL, RequestMethod.POST);
    request.add("nodeRecordID", nodeRecordID);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        CommonResult result = JsonUtil.jsonToObject(response.get(), CommonResult.class);
        if (result == null) return;
        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 上传无地磁临停单图片
   */
  public static void uploadNodePic(final Context mContext, String nodeRecordID, String pic, final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NPNODE_UPLOAD_NODE_PIC, RequestMethod.POST);
    request.add("nodeRecordID", nodeRecordID);
    request.add("pic", pic);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        CommonResult result = JsonUtil.jsonToObject(response.get(), CommonResult.class);
        if (result == null) return;
        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 获取申请驶离的停车单列表
   */
  public static void npnode_record_apply_list(final Context mContext, int type,
      String carNumber, String handleDate, int page, int rows,
      final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NPNODE_RECORD_APPLY_LIST, RequestMethod.POST);
    request.add("carNumber", carNumber);
    request.add("handleDate", handleDate); //yyyy-MM-dd
    request.add("lotID", PreferencesHelper.getSettingString(PreferencesHelper.User.Key.LOT_ID));
    request.add("type", type); //类型: 0:未处理 1:已处理
    request.add("page", page);
    request.add("rows", rows);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        APIM_NpNodeRecordList result =
            JsonUtil.jsonToObject(response.get(), APIM_NpNodeRecordList.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 管理员处理申请驶离的停车单
   */
  public static void npnode_record_apply_handle(final Context mContext, String nodeRecordID,
      int state, String pic, final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NPNODE_RECORD_APPLY_HANDLE, RequestMethod.POST);
    request.add("nodeRecordID", nodeRecordID);
    request.add("adminID", PreferencesHelper.userManager().getUserID());
    request.add("state", state);
    request.add("pic", pic);

    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        CommonResult result =
            JsonUtil.jsonToObject(response.get(), CommonResult.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }
}
